layui.use('form', function () {
    var form = layui.form;
    $(function () {
        $.ajax({
            url: '/teacher/' + sessionStorage.getItem('courseId'),
            type: 'GET',
            dataType: "JSON",
            success: function (res) {
                if (res.code === 0) {
                    let course = res.data;
                    $("#id").val(course.id);
                    $("#attendance").val(course.attendance);
                    $("#classPerformance").val(course.classPerformance);
                    $("#task").val(course.task);
                    $("#experiment").val(course.experiment);
                    $("#exam").val(course.exam);
                    $("#version").val(course.version);
                    form.render();
                }
            }
        })
    })

    // 自定义验证规则
    form.verify({
        id: function (value) {
            if (value.length < 1) {
                return 'id不可改！';
            }
        },
        attendance: [
            /[0-9]/
            , "占比必须为 0~1！"
        ],
        classPerformance: [
            /[0-9]/
            , "占比必须为 0~1！"
        ],
        task: [
            /[0-9]/
            , "占比必须为 0~1！"
        ],
        experiment: [
            /[0-9]/
            , "占比必须为 0~1！"
        ],
        exam: [
            /[0-9]/
            , "占比必须为 0~1！"
        ],
    });
    // 监听提交
    form.on('submit(demo1)', function () {
        // submit 提交事件
        let serialize = $("#scores-form").serialize();
        $.ajax({
            url: '/teacher/course/update/' + sessionStorage.getItem('courseId'),
            type: "PUT",
            data: serialize,
            dataType: "JSON",
            success: function (JSONResult) {
                if (JSONResult.code === 0) {
                    layer.msg("修改成功！");
                } else {
                    layer.msg("修改失败！");
                }
            }
        });
        return false;
    });
})
layui.use('table', function () {
    const table = layui.table;
    //第一个实例
    table.render({
        elem: '#courseStudent'
        , height: 'full'
        , url: '/teacher/course/students/' + sessionStorage.getItem('courseId')  //数据接口
        , parseData: function (res) {
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.data //解析数据列表
            }
        }
        , title: '学生成绩表'
        , cols: [[
            {field: 'id', title: 'ID', width: 100}
            , {field: 'studentId', title: '学号', width: 230}
            , {field: 'attendance', title: '考勤', width: 200}
            , {field: 'classPerformance', title: '课堂表现', width: 180}
            , {field: 'task', title: '作业', width: 90}
            , {field: 'experiment', title: '实验', width: 130}
            , {field: 'exam', title: '考试', width: 100}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 100, align: 'center'}

        ]]
        , page: false
    });


    table.on('tool(courseStudent)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            sessionStorage.setItem('Id', data.id);
            sessionStorage.setItem('studentId', data.studentId);
            layer.open({
                type: 2,
                title: data.studentId + ' 成绩栏',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area: ['800px', '520px'],
                content: '/pages/teacher/studentScore',
                cancel: function (index) {
                    layer.close(index);
                    table.render({
                        elem: '#courseStudent'
                        , height: 'full'
                        , url: '/teacher/course/students/' + sessionStorage.getItem('courseId')  //数据接口
                        , parseData: function (res) {
                            return {
                                "code": res.code, //解析接口状态
                                "msg": res.msg, //解析提示文本
                                "count": res.data.total, //解析数据长度
                                "data": res.data.data //解析数据列表
                            }
                        }
                        , title: '学生成绩表'
                        , cols: [[
                            {field: 'id', title: 'ID', width: 100}
                            , {field: 'studentId', title: '学号', width: 230}
                            , {field: 'attendance', title: '考勤', width: 200}
                            , {field: 'classPerformance', title: '课堂表现', width: 180}
                            , {field: 'task', title: '作业', width: 90}
                            , {field: 'experiment', title: '实验', width: 130}
                            , {field: 'exam', title: '考试', width: 100}
                            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 100, align: 'center'}

                        ]]
                        , page: false
                    });
                }
            });
        }
    });

})